package com.winchain.chainbill.tbill.basic.service.impl;

import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.state.StateEngine;
import com.shell.bee.state.StateException;
import com.shell.bee.state.engine.State;
import com.shell.bee.state.engine.service.StateMachineService;
import com.shell.framework.utils.CommonDataUtil;
import com.winchain.chainbill.tbill.base.po.TbOpt;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.base.service.TbOptService;
import com.winchain.chainbill.tbill.basic.service.TBillOptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TBillOptServiceImpl implements TBillOptService {
	private static final String TRADE_BILL_STATE_FLOW_ID = "tradeBillStateFlow";
	@Autowired
	private TbOptService tbOptService;

	public void operate(TbRec bill, TbOpt opt) {
		String curState = bill.getBillState();
		String nextState = "";
		String eventCode = opt.getOptCode();
		try {
			State state = StateEngine.getStateMachineEngine().fire("tradeBillStateFlow", curState, eventCode);
			nextState = state.getCode();
		} catch (StateException e) {
			throw new RuntimeException("����������������������", e);
		}
		int optSeq = bill.getOptSeq().intValue();
		bill.setOptSeq(Integer.valueOf(++optSeq));

		opt.setOptId(SystemUtil.getUUID());
		opt.setBillId(bill.getBillId());
		opt.setCurState(curState);
		opt.setNxtState(nextState);
		opt.setOptSeq(Integer.valueOf(optSeq));
		opt.setOprId(CommonDataUtil.getUserId());
		opt.setOprNm(CommonDataUtil.getUserName());
		opt.setOptTime(DateUtil.getSystemDateTime());
		this.tbOptService.save(opt);
	}
}
